package com.base.simple.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.base.simple.Constants;
import com.base.simple.R;
import com.base.simple.main.event.ChangeStyleEvent;
import com.base.simple.main.fragment.SimpleFragment;
import com.dream.base.eventbus.EventBusManager;
import com.dream.base.tools.SharedPreferencesUtil;
import com.dream.base.ui.BaseActivity;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;


public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private ViewPager fragmentContainer;

    private BottomNavigationBar mBottomNavigationBar;

    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    private Toolbar mToolBar;

    @Override
    protected void beforeLoadActivity(Bundle savedInstanceState) {
        super.beforeLoadActivity(savedInstanceState);

        int aLong = SharedPreferencesUtil.getInt(this, Constants.CURRENT_STYLE, 0);
        if (aLong != 0) {
            setTheme(aLong);
        }
    }

    @Override
    protected int setLayoutRes() {
        EventBusManager.getInstance().register(this);
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {

        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.ic_favorite, "One"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_gavel, "Two"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_grade, "Three"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_group_work, "Four"))
                .initialise();


        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                fragmentContainer.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        mFragmentList.add(new SimpleFragment());
        mFragmentList.add(new SimpleFragment());
        mFragmentList.add(new SimpleFragment());
        mFragmentList.add(new SimpleFragment());

        fragmentContainer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mBottomNavigationBar.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        fragmentContainer.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        });

        StatusBarUtil.setColor(this, getDarkColorPrimary(), 0);
    }

    private int getDarkColorPrimary() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.styleColor, typedValue, true);
        return typedValue.data;
    }

    @Override
    protected void findViews() {

        fragmentContainer = (ViewPager) findViewById(R.id.fragment_container);
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        mToolBar = (Toolbar) findViewById(R.id.toolbar);

    }

    @Override
    protected MainPresenter initPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected void setListeners() {

    }

    @Override
    public void viewTest() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusManager.getInstance().unregister(this);
    }

    @Subscribe
    public void onEvent(ChangeStyleEvent event) {
        mToolBar.setBackgroundColor(ContextCompat.getColor(this, event.getPrimaryColor()));
        int currentSelectedPosition = mBottomNavigationBar.getCurrentSelectedPosition();
        mBottomNavigationBar.setBarBackgroundColor(event.getPrimaryColor()).setFirstSelectedPosition(currentSelectedPosition).initialise();
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, event.getPrimaryColor()), 0);
    }

}
